﻿using System;
using System.Web.Security;
using SmartHRMS.Infrastructure;
using StructureMap;
using SmartHRMS.Model.Account;

namespace SmartHRMS.UI.Web
{
    public partial class LoginPage : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // Put user code to initialize the page here
            if ((Request["ShowError"] == "true") || (Request["ShowError"] == "1"))
            {
                AuthRedirectFailure.Text = "You did not have sufficient permission to view the previous page. Please login below.";
                AuthRedirectFailure.Visible = true;
            }
            else
            {
                AuthRedirectFailure.Visible = false;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            IPrincipalService _ICustomPrincipalService = ObjectFactory.GetInstance<IPrincipalService>();

            try
            {
                LoginResults loginResults = _ICustomPrincipalService.Login(txtUserName.Text.ToString(), txtPassword.Text.ToString());

                switch (loginResults)
                {
                    case LoginResults.InvalidCredential:
                        LoginResult.Text = "Login failed for " + txtUserName.Text;
                        LoginResult.Visible = true;
                        break;
                    case LoginResults.AccountDisabled:
                        LoginResult.Text = "Your account is disabled.Please contact the system admin.";
                        LoginResult.Visible = true;
                        break;
                        //I will implement it soon,remaind me anyone else who see this.
                        //  case LoginResults.LockedOut:
                        //      LoginResult.Text = "UserIsLockedOut";
                        //      break;
                    case LoginResults.Successful:
                        FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text, true);
                        break;
                }
            }
            catch (Exception exp)
            {
            }
        }
    }
}